<?php
	class Location extends MY_Controller{
		
		public function __construct(){
			parent::__construct();
			$this->load->model('location_model');
			$this->load->model('country_model');
			$this->load->model('city_model');
			$this->load->model('district_model');
		}
		
		public function index($limit=FALSE){	
			//set the limit or offset for each page
			$this->location_model->pager($limit);
			$my_link = "";
			
			// define the link for pagination
			$my_link = site_url('location');
			
			$data['location'] = $this->location_model->get_location();
			
			// call the pagination from parent controller			
			$this->pager($my_link, $this->location_model->get_total(), $this->location_model->get_record_per_page());
			
			$data['title'] = "Location list";
			$this->load->view('templates/header1', $data);
			$this->load->view('location/index', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');			
		}
		
		public function displayForm($id=FALSE){			
				$this->load->helper("form");
				$data = array();
				if($id!=FALSE){
					$data['location'] = $this->location_model->get_location($id);				
				}
				
				// Get Country list
				$data['country'] 	= $this->country_model->get_country_list();
				
				// Get city list if the country id exists in location array
				if(isset($data['location']['iso']))
					$data['city'] = $this->city_model->get_city_list($data['location']['iso']);
					
				// Get district list if the city id exists in location array
				if(isset($data['location']['ci_id']))
					$data['district'] = $this->district_model->get_district_list($data['location']['ci_id']);
				
				$data['title']="Create Location";
				$this->load->view('templates/header1', $data);
				$this->load->view('location/form', $data);
				$this->load->view('templates/right', $data);
				$this->load->view('templates/footer');
			
		}
		
		public function create(){
			$this->displayForm();
		}
		
		public function edit($id){
			$this->displayForm($id);
		}
		
		public function delete($id){
			$this->location_model->delete($id);
			redirect("location", "refresh");
		}
		
		public function save(){			
			
				$this->load->helper("form");
				$this->load->library('form_validation');
				$this->form_validation->set_rules('address', 'Address', 'required');
				if ($this->form_validation->run() === FALSE){
					$data['title']="Location group";
					$this->load->view('templates/header1', $data);
					$this->load->view('location/form', $data);
					$this->load->view('templates/right', $data);
					$this->load->view('templates/footer');
				}
				else{
					$this->location_model->save();
					redirect("location", "refresh");
				}
			
		}	
		
		public function get_city(){			
			$country_id = $this->input->post('cid');
			$arr = $this->city_model->get_city_list($country_id);
			if(count($arr)) echo json_encode($arr);
			else echo json_encode(array('1'=>'no data'));
		}
		
		public function get_district(){
			$city_id = $this->input->post('cid');
			$arr = $this->district_model->get_district_list($city_id);
			if(count($arr)) echo json_encode($arr);
			else echo json_encode(array('1'=>'no data'));
		}
		
	}
?>